package com.goldsprite.spinelearn;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.goldsprite.spinelearn.examples.Env;
import com.goldsprite.spinelearn.examples.SpineCharacter;

public class GdxLauncher extends ApplicationAdapter {
	private SpineCharacter character;
	private PolygonSpriteBatch batch;
	private OrthographicCamera camera;
	private Viewport viewport;

	// 固定虚拟分辨率
	private final float VIRTUAL_WIDTH = Env.WORLD_WIDTH;
	private final float VIRTUAL_HEIGHT = Env.WORLD_HEIGHT;

	@Override
	public void create() {
		batch = new PolygonSpriteBatch();

		// 设置相机
		camera = new OrthographicCamera();
		viewport = new FitViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, camera);
		camera.setToOrtho(false, Env.WORLD_WIDTH, Env.WORLD_HEIGHT);

		// 创建角色（固定使用骑士）
		character = new SpineCharacter(Env.getCurrentCharacterType());
		character.create();

		// 调整角色位置到视口中心
		character.setPosition(VIRTUAL_WIDTH / 2, VIRTUAL_HEIGHT / 2);
	}

	@Override
	public void render() {
		// 清屏为灰色背景
		ScreenUtils.clear(Color.GRAY);

		float delta = Gdx.graphics.getDeltaTime();

		logic();

		// 更新
		character.update(delta);

		// 渲染
		batch.setProjectionMatrix(camera.combined);
		character.render(batch);
	}

	private void logic() {
	}

	@Override
	public void resize(int width, int height) {
		viewport.update(width, height);
	}

	@Override
	public void dispose() {
		batch.dispose();
		character.dispose();
	}
}
